package com.aode.music.view.activity;

import android.annotation.SuppressLint;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.aode.common.bean.request.LoginData;
import com.aode.common.bean.web.Result;
import com.aode.music.R;
import com.aode.music.databinding.ActivityRegisterBinding;
import com.aode.music.viewmodel.LoginViewModel;
import com.aode.music.viewmodel.RegisterViewModel;
import com.aode.utils.main.ToastUtils;

public class RegisterActivity extends AppCompatActivity {

    private ActivityRegisterBinding registerBinding;
    private RegisterViewModel registerViewModel;
    private LoginData loginData = new LoginData();
    private String username;
    private String password;
    private String repassword;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerBinding = DataBindingUtil.setContentView(this, R.layout.activity_register);
        registerViewModel = ViewModelProviders.of(this).get(RegisterViewModel.class);
        registerBinding.setRegister(RegisterActivity.this);


    }



    private boolean checkNetwork() {
        ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if(networkInfo!=null){
            return  true;
        }else{
            return false;
        }
    }

    private void initListener() {
        registerViewModel.getRegister().observe(this, result -> {
            if(result.getCode().equals("0")){
                ToastUtils.show(getApplicationContext(),"注册成功");
            }else{
                if(result.getMsg()!=null){
                    ToastUtils.show(getApplicationContext(),result.getMsg());
                }else{
                    ToastUtils.show(getApplicationContext(),"注册失败");
                }
            }
        });
    }

    @SuppressLint("NonConstantResourceId")
    public void click(View view){
        switch (view.getId()){
            case R.id.buttonSignUp:
                boolean b = checkNetwork();
                if(b){
                    boolean a = checkRegister();
                    register(a);
                    Log.e("TAG","注册啦");
                }else{
                    ToastUtils.show(getApplicationContext(),"请检查网络");
                }

                break;
            case R.id.textSignIn:
                finish();
                break;
        }
    }

    private void register(boolean a) {
        if(a){
            loginData.setPassword(password);
            loginData.setUsername(username);
            registerViewModel.register(loginData);
            initListener();
        }
    }

    private boolean checkRegister() {
        username = registerBinding.inputName.getText().toString().trim();
        password = registerBinding.inputPassword.getText().toString().trim();
        repassword = registerBinding.inputConfirmPassword.getText().toString().trim();
        if(username.equals("")){
            ToastUtils.show(getApplicationContext(),"用户名不能为空");
            return false;
        }else if(password.equals("")){
            ToastUtils.show(getApplicationContext(),"密码不能为空");
            return false;
        }else if(!password.equals(repassword)){
            ToastUtils.show(getApplicationContext(),"密码与确认密码不一致请重试");
            return false;
        }
        return true;
    }
}
